package cn.neusoft.edu.his.demo.controller;

import io.swagger.annotations.Api;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.neusoft.edu.his.demo.common.Result;
import cn.neusoft.edu.his.demo.mapper.DoctorWorkMapper;
import cn.neusoft.edu.his.demo.mapper.WorkPlanMapper;
import cn.neusoft.edu.his.demo.po.DoctorWork;
import cn.neusoft.edu.his.demo.po.DoctorWorkParameter;
import cn.neusoft.edu.his.demo.po.FullDoctorWork;
import cn.neusoft.edu.his.demo.po.WorkPlan;
import cn.neusoft.edu.his.demo.service.WorkPlanService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

@CrossOrigin
@Api("WorkPlanController相关的api")
@RestController
public class WorkPlanController {
	@Autowired
	private WorkPlanMapper workPlanMapper;
	@Autowired
	private DoctorWorkMapper doctorworkMapper;
	@Autowired
	private WorkPlanService workPlanService;
	
	/*
	 * 周一到周日的计划大纲
	 * */
	@RequestMapping("/getPlansByWeek")
	public Result getPlansByWeek(String week){
		List<DoctorWork> list=workPlanMapper.getWorkPlanByWeek(week);
		return Result.ok(list);
	}
	
	/*
	 * 周一到周日的计划大纲
	 * */
	@RequestMapping("/getPlansStatus2")
	public Result getPlansStatus2(String startDay){
		Date d=this.getDate(startDay);
		WorkPlan wp=workPlanService.getPlansStatus(d);
		return Result.ok(wp);
	}
	
	/*
	 * 周一到周日的计划大纲
	 * */
	@RequestMapping("/getPlansStatus")
	public WorkPlan getPlansStatus(String startDay){
		Date d=this.getDate(startDay);
		return workPlanService.getPlansStatus(d);
	}
	
	/*
	 * 生成周一到周日的计划大纲
	 * */
	@RequestMapping("/generatePlans")
	public Result generatePlans(String startDay){
		Date d=this.getDate(startDay);
		WorkPlan wp=new WorkPlan();
		wp.setDay(d);
		wp.setStatus(0);//初始状态
		wp.setCreateBy("admin");
		wp.setWeekday("1");
		
		boolean res=workPlanService.generatePlan(wp);
		if(res)
			return Result.ok("生成成功");
		else
			return Result.fail("生成失败");
	}
	
	/*
	 * 修改计划大纲的状态
	 * */
	@RequestMapping("/updatePlanStatus")
	public Result updatePlanStatus(String day,int status){
		Date d=this.getDate(day);
		WorkPlan wp=new WorkPlan();
		wp.setDay(d);
		wp.setStatus(status);//初始状态
		int res=workPlanService.updateByDay(wp);
		if(res>0)
			return Result.ok("状态修改成功");
		else
			return Result.fail("状态修改失败");
	}
	
	/*
	 * 获取当前周的所有医生排班情况
	 * */
	@RequestMapping("/getDoctorWorks")
	public Result getDoctorWorks(String day){
		List<FullDoctorWork> list=workPlanService.getAllBaseInfo(day);
		return Result.ok(list);
	}
	@RequestMapping("/updateDoctorWorks")
	public Result updateDoctorWorks(String day,int departmentId,DoctorWorkParameter parameter){		
		List<DoctorWork> doctors=parameter.getDoctorworks();
		//System.out.println(doctors.toString());
		Date d=this.getDate(day);
		int year=1900+d.getYear();
		int month=1+d.getMonth();
		int date=d.getDate();
		WorkPlan wp=this.workPlanMapper.selectByDay(year, month, date);
		int res=0;
		for(DoctorWork doctor:doctors){
			if(doctor.getDepartId1()==departmentId)
			{
				
				QueryWrapper<DoctorWork> wrapper = new QueryWrapper<DoctorWork>();
			    wrapper.eq("day", day);
			    wrapper.eq("doc_id", doctor.getDocId());
			    doctorworkMapper.delete(wrapper);
			    doctor.setDay(day);
			    doctor.setWeekday(wp.getWeekday());
			    doctor.setFeeId(doctor.getFeeId());
			    int countLeft=0;
			    if(doctor.getAmwork()!=null&&!doctor.getAmwork().equals("0"))
			    	countLeft+=30;
			    if(doctor.getPmwork()!=null&&!doctor.getPmwork().equals("0"))
			    	countLeft+=30;
			    doctor.setCountLeft(countLeft);//设置可挂号数量
			    doctorworkMapper.insert(doctor);
			    res++;
			}
		}
		if(res>0)
			return Result.ok("保存成功");
		else
			return Result.fail("保存失败");
	}
	
	/**
	 * 字符串转换成时间类型
	 * @param str
	 * @return
	 */
	public Date getDate(String str) {
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	    Date date = null;
	    try {
	        date = dateFormat.parse(str);
	    } catch (ParseException e) {
	        e.printStackTrace();
	    }
	    return date;
	}
}
